package com.ruoyi.shop.clientuser.history.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mall.common.domain.DtsUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.common.domain.DtsSearchHistory;
import com.ruoyi.shop.clientuser.history.service.IDtsSearchHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 搜索历史Controller
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@RestController
@RequestMapping("/clientuser/search/history")
public class DtsSearchHistoryController extends BaseController
{
    @Autowired
    private IDtsSearchHistoryService dtsSearchHistoryService;

    /**
     * 查询搜索历史列表
     */
    @PreAuthorize("@ss.hasPermi('search:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(String username, String keyword)
    {
        startPage();
        DtsSearchHistory dtsSearchHistory = new DtsSearchHistory();
        DtsUser dtsUser = new DtsUser();
        dtsUser.setUsername(username);
        dtsSearchHistory.setDtsUser(dtsUser);
        dtsSearchHistory.setKeyword(keyword);
        List<DtsSearchHistory> list = dtsSearchHistoryService.selectDtsSearchHistoryList(dtsSearchHistory);
        return getDataTable(list);
    }

    /**
     * 导出搜索历史列表
     */
    @PreAuthorize("@ss.hasPermi('search:history:export')")
    @Log(title = "搜索历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtsSearchHistory dtsSearchHistory)
    {
        List<DtsSearchHistory> list = dtsSearchHistoryService.selectDtsSearchHistoryList(dtsSearchHistory);
        ExcelUtil<DtsSearchHistory> util = new ExcelUtil<DtsSearchHistory>(DtsSearchHistory.class);
        util.exportExcel(response, list, "搜索历史数据");
    }

    /**
     * 获取搜索历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('search:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dtsSearchHistoryService.selectDtsSearchHistoryById(id));
    }

    /**
     * 新增搜索历史
     */
    @PreAuthorize("@ss.hasPermi('search:history:add')")
    @Log(title = "搜索历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DtsSearchHistory dtsSearchHistory)
    {
        return toAjax(dtsSearchHistoryService.insertDtsSearchHistory(dtsSearchHistory));
    }

    /**
     * 修改搜索历史
     */
    @PreAuthorize("@ss.hasPermi('search:history:edit')")
    @Log(title = "搜索历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtsSearchHistory dtsSearchHistory)
    {
        return toAjax(dtsSearchHistoryService.updateDtsSearchHistory(dtsSearchHistory));
    }

    /**
     * 删除搜索历史
     */
    @PreAuthorize("@ss.hasPermi('search:history:remove')")
    @Log(title = "搜索历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dtsSearchHistoryService.deleteDtsSearchHistoryByIds(ids));
    }
}
